<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="danger" @click="deleteByIds">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      <el-upload :action="'http://' + serverIp + ':9091/files/upload'" :on-success="suc" :show-file-list="false"
                 style="display: inline-block">
        <el-button type="primary" class="ml-5">上传文件<i class="el-icon-top"></i></el-button>
      </el-upload>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="文件名称"></el-table-column>
      <el-table-column prop="type" label="文件类型"></el-table-column>
      <el-table-column prop="size" label="文件大小(kb)"></el-table-column>
      <el-table-column label="启用/禁用">
        <template slot-scope="scope">
        <el-switch
            v-model="scope.row.enable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="ChangeEnable(scope.row)">
        </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="下载">
        <template slot-scope="scope">
          <el-button type="primary" @click="download(scope.row.url)">下载 <i class="el-icon-bottom"></i></el-button>
          <el-button type="danger" @click="deleteById(scope.row.id)">删除 <i class="el-icon-delete"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {serverIp} from "../../public/config";

export default {
  name: "Files",
  data() {
    return {
      serverIp :serverIp,
      tableData: [],
      multipleSelection: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: '',
      type: '',
      size: '',
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/files/findPage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
          type: this.type,
          size: this.size,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleSizeChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleCurrentChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    deleteByIds() {
      this.$confirm('此操作将批量删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
        this.request.post("/files/del/ids", ids)
        this.$message.success("批量删除成功")
        this.load()
      }).catch(() => {
        this.$message.error("已取消批量删除")
      });
    },
    deleteById(id) {
      this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.request.delete("/files/" + id)
        this.$message.success("删除成功")
        this.load()
      }).catch(() => {
        this.$message.error("取消删除")
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    download(url) {
      window.open(url);
    },
    suc() {
      this.$message.success("导入成功")
      this.load()
    },
    ChangeEnable(row){
      this.request.post("/files/update",row).then(res => {
        if (res.code === '200'){
          this.$message.success("操作成功")
        }
      })
    },
  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>